#include "VariableTable.h"
#include "Exception.hpp"

namespace cal
{

bool VariableTable::exist(const Identifier& name) const
{
    return _hash_table.count(name);    
}

header::Number VariableTable::get_value(const Identifier& name) const
{
    if (!exist(name)) throw Exception{"not find variable: " + name.str()};
    return _hash_table.at(name);
}

void VariableTable::add(const Identifier& name, const header::Number& value) 
{
    _hash_table[name] = value;
    _added_id  = name;
    _added_val = value;
}

void VariableTable::print(std::ostream& os, int num_per_line)
{
    os << "VariableTable {\n";
    int cnt = 0;
    for (auto x : _hash_table) {
        ++cnt;
        os << x.first << " = " << x.second << ",  ";
        if (cnt % num_per_line == 0) os << "\n";
    }
    os << "\n};";
}

}   // namespace cal
